import axios from "axios";
import { ElMessage } from "element-plus";
import useUserStore from "@/store/modules/user.ts";
const request = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 10000
})

// 请求拦截器
request.interceptors.request.use(config => {
    let userStore = useUserStore()
    config.headers.token = userStore.token
    return config
})

// 响应拦截器
request.interceptors.response.use(response => {
    return response.data
}, error => {
    let message = error.message
    let status = error.response?.status
    if (!status) {
        ElMessage({
            type: 'error',
            message: message
        })
        return Promise.reject(error)
    }
    switch (status) {
        case 2000:
            message = '用户信息已过期，请重新登录'
            break
    }
    ElMessage({
        type: 'error',
        message: message
    })
    return Promise.reject(error)
})

export default request
